define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xilujob.job_interview/index' + location.search,
                    detail_url: 'xilujob.resume/detail',
                    invite_url: 'xilujob.job_interview/create_interview',
                    del_url: '',
                    table: 'xilujob_resume',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'updatetime',
                search: false, //是否启用快速搜索
                commonSearch: true, //是否启用通用搜索
                searchFormVisible: true, //是否始终显示搜索表单
                columns: [
                    [
                        {checkbox: true},
                        {field: 'resume.id', title: __('Id'), operate:false,visible:false},
                        {field: 'job_id', title: __('职位'),searchList:Config.jobs, formatter: Table.api.formatter.normal},
                        {field: 'person',title:'基本信息',align:'left',formatter: Controller.api.formatter.personal , operate: false},
                        {field: 'work_experience', title: __('工作经验'),align:'left', formatter:Controller.api.formatter.experience, operate:false},
                        {field: 'interview_time', title: __('面试时间'),operate:'RANGE',addclass:'datetimerange',formatter: Table.api.formatter.datetime},
                        {field: 'notice', title: __('备注'),operate:false},
                        {field: 'status', title: __('应聘状态'),searchList:{0:__('已邀请'),1:__('拒绝邀请'),2:__('同意邀请'),3:__('面试失败'),4:__('录用')},formatter: Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'),buttons: [
                                {
                                    name: 'interview_agree',
                                    title: '录用',
                                    text: '录用',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-click',
                                    icon: '',
                                    click: function (option,row,button) {
                                        Layer.confirm('确认录用？', function(index) {
                                            Fast.api.ajax({
                                                url: 'xilujob.job_interview/interview_status', data: {interview_id:row.id,status:4}
                                            }, function(data,ret) {
                                                $(".btn-refresh").trigger("click");
                                            });
                                            Layer.close(index);
                                        });
                                    },
                                    visible: function (row) {
                                        return row.status===2;
                                    }
                                },
                                {
                                    name: 'interview_disagree',
                                    title: '不合适',
                                    text: '不合适',
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    icon: '',
                                    click: function (option,row,button) {
                                        Layer.confirm('确认不合适？', function(index) {
                                            Fast.api.ajax({
                                                url: 'xilujob.job_interview/interview_status', data: {interview_id:row.id,status:3}
                                            }, function(data,ret) {
                                                $(".btn-refresh").trigger("click");
                                            });
                                            Layer.close(index);
                                        });
                                    },
                                    visible: function (row) {
                                        return row.status===2;
                                    }
                                },
                            ], table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);


        },

        create_interview: function() {
            Controller.api.bindevent();
        },
        batch_invite: function () {
            this.create_interview();
        },
        api: {
            formatter: {
                personal:function(value,row) {

                    var resume_data = row.resume.personal.age+' | '
                        + (row.resume.personal.work_experience ? row.resume.personal.work_experience.name : '') + ' | '
                        + (row.resume.personal.education_level ? row.resume.personal.education_level.name : '') + ' | '
                        + (row.resume.personal.current_state ? row.resume.personal.current_state.name : '');
                    var summary_content = row.resume.summary.content;
                    summary_content = summary_content.length>20 ? summary_content.substring(0,20)+'...' : summary_content;
                    var html = '<div class="media">';
                    html += '<a class="media-left" href="#">';
                    html += '<img width="50px" height="50px" src="'+Fast.api.cdnurl(row.resume.personal.avatar)+'" class="img-circle" alt="">';
                    // html += '<div>'+row.resume.job_intention.expect_salary.name+'</div>';
                    html += '</a>';

                    html += '<div class="media-body" style="width: auto">';
                    html += '<a href="'+Fast.api.fixurl('xilujob.resume/detail?ids='+row.resume.id)+'" class="btn-dialog" data-title="简历详情"><h4>'+row.resume.personal.realname+'</h4>';
                    html += '<p>'+resume_data+'</p></a>';
                    // html += '<p>求职期望：'+row.resume.job_intention.fav_city.allname+'</p>';
                    html += '<p><a title="'+row.resume.summary.content+'" style="width: 150px">'+summary_content+'</a></p>';
                    html += '</div>';
                    html += '</div>';
                    return html;
                },
                experience: function(value,row){
                    if(!row.resume.works) return '';
                    var html = '';
                    for (var i=0;i<row.resume.works.length;i++){
                        var end = row.resume.works[i].end ? row.resume.works[i].end : "至今";
                        html += '<h5>'+row.resume.works[i].start+'-'+end+'   '+row.resume.works[i].company_name+' . '+row.resume.works[i].job_title+'</h5>';
                    }
                    return html;
                }
            },
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});